<?php
/** UnivIS update script
 * Fetches content from UnivIS via PRG interface in XML and stores 
 * it into a mysql database
 */

require_once t3lib_extMgm::extPath('univis2typo3').'/mod1/parser/modules/generic_module.php';
$lecturesTruncated = false;

// Note: It's sometimes hard to distinguish between 
// - the running semester.
//   This term shall refer to the semester which is currently running at the university.
//   E.g. in Mai 2009 it will consist of 2009 and 's'
// - the current semester.
//   This term shall refer to the current state of this module. It's the semester which just got fetched or will be fetched next.
class LecturesModule extends GenericModule
{
    var $year;
    var $sem;

    function LecturesModule($module)
    {
	global $lecturesTruncated;

	$this->module = $module;
	// starting semester for univis data
	$this->year = 2000;
	$this->sem  = 's';

	if (!$lecturesTruncated)
	{
	    $sql = 'TRUNCATE TABLE u_lecture_term';
	    mysql_query($sql);
	    $lecturesTruncated = true;
	}
    }

    // getRunningSemester returns the running semster.
    function getRunningSemester() {
	$m = (int) date('m');
	$y = date('Y');
	$term = 's';

	if ($m <= 3) {
	    // January till March -> still winter term of the previous year
	    --$y;
	    $term = 'w';
	} else if ($m >= 9) {
	    // September till Dezember -> winter term of the current year
	    $term = 'w';
	}
	// else -> summer term of the current year

	// Do NOT update $this->year and $this->sem
	// they store the current state of this object, not of reality

	return array('year' => $y, 'sem' => $term);
    }

    // getNextSemester computes the semester following a given one.
    // If no reference semester is given it returns the semester following the running one.
    function getNextSemester($semester = null) {
	if (is_null($semester)) {
	    $semester = LecturesModule::getRunningSemester();
	}
	
	if ('w' == $semester['sem']) {
	    ++$semester['year'];
	    $semester['sem'] = 's';
	} else {
	    $semester['sem'] = 'w';
	}

	return $semester;
    }

    function getUrl($uConf)
    {
	$semester = $this->year.$this->sem;
	$GLOBALS['semester'] = $semester;
	return $uConf['prg_url']
		. 'department='.$uConf['department']
		. '&search='.$this->module
		. '&show=xml'
		. '&sem='.$semester;
    }

    // hasMoreUrls fulfills two functions.
    // 1) It checks if we want to fetch more XML data (for another semester)
    //    We want to fetch data for previous semesters, the running semester and the one after it.
    // 2) If we want to fetch more, it updates the state of the object.
    function hasMoreUrls()
    {
	$GLOBALS['semester'] = '';
	unset($GLOBALS['semester']);

	$currentSemester = array('year' => $this->year, 'sem' => $this->sem);

	// the last semester to fetch
	$lastSemester = LecturesModule::getNextSemester();
	//$lastSemester = '2009s';

	// the next semester to fetch
	$nextSemester = LecturesModule::getNextSemester($currentSemester);

	// check if nextSemester is later than lastSemester
	if ($nextSemester['year'] > $lastSemester['year']
	    || ($nextSemester['year'] == $lastSemester['year'] && 'w' == $nextSemester['sem'] && 's' == $lastSemester['sem'])) {
	    return false;
	}

	$this->year = $nextSemester['year'];
	$this->sem  = $nextSemester['sem'];

	return true;

	/* legacy code, kept for reference.
	// may be deleted if the new method works
	if ('w' == $this->sem) {
	    ++$this->year;
	    $this->sem = 's';
	} else {
	    $this->sem = 'w';
	}
	// check for the last semester
	if ($this->year >= (date('Y')+1)) {
	    return false;
	}
	return true;
	*/
    }
}
?>
